﻿/*
 * User: artem
 * Date: 02.05.2011
 * Time: 13:44
 * 
 */
using System;
using System.Collections.Specialized;
using System.Xml.Linq;

namespace SettingManager
{
	/// <summary>
	/// Description of MyClass.
	/// </summary>
	public class XMLSettingManager
	{
		private const string root_name = @"serrings";
		private StringDictionary settings = new StringDictionary();
		
		public XMLSettingManager()
		{
			//
		}
		
		public static XMLSettingManager Load(string config)
		{
			XMLSettingManager instance = new XMLSettingManager();
			
			XElement root = XElement.Load(config);
				
			foreach (var it in root.Attributes())
				instance.Add(it.Name.LocalName, it.Value);
			
			return instance;
		}
		
		public void Save(string config)
		{
			XElement root = new XElement(root_name);
			
			foreach (string it in settings)
				root.Add(new XAttribute(it, settings[it]));
			
			root.Save(config);
		}
		
		public string this[string key]
		{
			get { return settings[key]; }
			set { settings[key] = value; }
		}
		
		public void Remove(string key)
		{	settings.Remove(key);	}
		
		public void Add(string key, string val)
		{	settings.Add(key, val);	}
		
		public void Clear()
		{	settings.Clear();	}
	}
}